﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using Wjtour.BLL;

namespace Wjtour.Web.Front
{
    public partial class memberinfoedit : MemberBasePage
    {
        protected TextBox realname;
        protected TextBox username;
        protected RadioButton male, female;
        protected TextBox email;
        protected DropDownList year, month, day;
        protected TextBox address, mobile, tel;
        protected Button submit;

        protected void Page_Load(object sender, EventArgs e)
        {
            submit.Click += submit_Click;
            if (!IsPostBack)
            {
                bindDropdown();

                realname.Text = CurrentMember.realname;
                username.Text = CurrentMember.username;
                male.Checked = CurrentMember.gender == null ? false : CurrentMember.gender.ToUpper() == "M";
                female.Checked = !male.Checked;
                email.Text = CurrentMember.email;
                address.Text = CurrentMember.address;
                mobile.Text = CurrentMember.mobile;
                tel.Text = CurrentMember.tel;
                year.SelectedValue = CurrentMember.birthday_year.ToString();
                month.SelectedValue = CurrentMember.birthday_month.ToString();
                day.SelectedValue = CurrentMember.birthday_day.ToString();
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            Wjtour.BLL.member_bll bll = new member_bll();
            var member = bll.Load(CurrentMember.id);
            member.username = username.Text.Trim();
            member.gender = male.Checked ? "M" : "F";
            member.email = email.Text.Trim();
            member.address = address.Text.Trim();
            member.tel = tel.Text.Trim();
            member.mobile = mobile.Text.Trim();
            member.realname = realname.Text.Trim();
            member.birthday_year = Utils.StrToInt(year.SelectedItem.Text, 0);
            member.birthday_month = Utils.StrToInt(month.SelectedItem.Text, 0);
            member.birthday_day = Utils.StrToInt(day.SelectedItem.Text, 0);

            bool b = bll.Save(member);
            if (b)
            {
                Session["CurrentMember"] = bll.Load(CurrentMember.id);
                Utils.Alert(this.Page, "个人信息修改成功！");
            }
            else
            {
                Utils.Alert(this.Page, "个人信息修改失败！");
            }
        }


        void bindDropdown()
        {
            for (int i = DateTime.Now.Year; i >= DateTime.Now.Year - 100; i--)
            {
                year.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i <= 12; i++)
            {
                month.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i <= 31; i++)
            {
                day.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
    }
}
